﻿using System;
using System.Data.SqlClient;
using System.Windows.Forms;
using DataCenter.Interface;
using DataCenter.Interface.Model;

namespace DataCenter.Component.Adapter
{
    [Adapter("VerticalAdapter", "1.0.0.1", "纵向数据提取适配器")]
    public class VerticalAdapterEntity : ComponentEntity
    {
        public VerticalAdapterEntity()
        {
            base.ComponentType = ComponentTypes.Adapter;
        }

        private string sqlString;
        private string keyField;
        private string timeField;
        private string valueField;

        [Parameter(StorageType = StorageTypes.StoreInContent)]
        public string SqlString
        {
            get { return sqlString; }
            set { sqlString = value; }
        }

        [Parameter]
        public string KeyField
        {
            get { return keyField; }
            set { keyField = value; }
        }

        [Parameter]
        public string TimeField
        {
            get { return timeField; }
            set { timeField = value; }
        }

        [Parameter]
        public string ValueField
        {
            get { return valueField; }
            set { valueField = value; }
        }

        public override bool Create(SqlCommand command)
        {
            VerticalAdapterEditForm editForm = new VerticalAdapterEditForm(this, false);
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                ComponentConfig = ToXml().OuterXml;
                Insert(command);
                return true;
            }
            return false;
        }

        public override bool Modify(SqlCommand command)
        {
            VerticalAdapterEditForm editForm = new VerticalAdapterEditForm(this, true);
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                base.ComponentConfig = ToXml().OuterXml;
                Update(command);
                return true;
            }
            return false;
        }

        public override TComponent CreateComponent<TComponent>(SqlCommand command)
        {
            IAdapter component = new VerticalAdapter(this);
            return (TComponent)component;
        }
    }
}
